/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package familytree.struts.actions;

import familytree.negocio.IAlbumProcessor;
import familytree.negocio.IImagenProcessor;
import familytree.negocio.*;
import familytree.negocio.bo.AlbumBO;
import familytree.negocio.bo.ImagenBO;
import familytree.negocio.bo.NodoBO;
import familytree.persistencia.pojos.Album;
import familytree.persistencia.pojos.Imagen;
import familytree.persistencia.pojos.Nodo;
import familytree.persistencia.pojos.Usuario;
import familytree.struts.forms.MediaForm;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author Antonio
 */
public class MediaAction extends Action {

    /* forward name="ok" path="" */
    private final static String OK = "ok";

    /*forward name = "fallo" path=""*/
    private final static String FALLO = "fallo";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        IAlbumProcessor albumBO = new AlbumBO();
        IImagenProcessor imagenBO = new ImagenBO();
        String accion = request.getParameter("accionSeleccionada");
       
         Nodo nodo = (Nodo) request.getSession().getAttribute("nodoActual");
        //Si hay que añadir albutienes
         if(accion.toLowerCase().equals("hacerprincipal")){
             String idImagen = request.getParameter("imagenSeleccionada");
          
             Imagen imagen = imagenBO.getImagenById(idImagen);
             nodo.setImagen(imagen);
             INodoProcessor nodoBO = new NodoBO();
             nodoBO.updateNodo(nodo);
             return mapping.findForward("hacerPrincipal");
        }
        if (accion.toLowerCase().equals("nuevoalbum")) {
            
            Album album = ((MediaForm) form).getAlbum();
            Usuario usuario = (Usuario) request.getSession().getAttribute("usuarioActual");
            
            if (album.getNombre().equals("") || album.getNombre() == null) {
                request.setAttribute("nombreVacio", null);
                return mapping.findForward(FALLO);
            }
            
            if (albumBO.existeAlbum(album.getNombre(), nodo)) {
                request.setAttribute("existeAlbum", true);
                return mapping.findForward(FALLO);
            }
            String ruta = request.getSession().getServletContext().getRealPath("");
            
            albumBO.insertaAlbum(album, usuario);
            
            List<Album> albumes = albumBO.getAlbumes(nodo);
            
            List<Imagen> imagenesRecientes = imagenBO.getFotosRecientes(nodo, ruta);
            
            Map<String,File> imagenesPortada = albumBO.getImagenesPortadas(nodo, ruta);
            
            request.setAttribute("albumes", albumes);
            request.setAttribute("imagenesPortada", imagenesPortada);
            request.setAttribute("imagenesRecientes", imagenesRecientes);
            
            return mapping.findForward("volver");
        } else //Si se entra en un album
        if (accion.contains("veralbum")) {

            String nombreAlbum = accion.substring(8);
            Album album = albumBO.getAlbum(nombreAlbum, nodo);
            request.setAttribute("album", album);
            return mapping.findForward(OK);
        }
        return mapping.findForward(OK);

    }
}